package cn.lr.basic.controller;

import cn.lr.basic.Exception.BusinessException;
import cn.lr.basic.service.IVerifyCodeService;
import cn.lr.basic.util.AjaxResult;
import cn.lr.basic.dto.RegisterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;
    @GetMapping("/image/{key}")
    public AjaxResult graph(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.graph(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
}
